package com.app.onebuy.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.onebuy.R;
import com.app.onebuy.basis.BasisActivity;
import com.app.onebuy.basis.BasisApp;
import com.app.onebuy.basis.Constants;
import com.app.onebuy.bean.GoodsTypeBean;
import com.app.onebuy.bean.OrderPaypalInfoBean;
import com.app.onebuy.bean.UserAccountInfoBean;
import com.app.onebuy.bean.UserChargeHistoryListData;
import com.app.onebuy.home.goods.info.GoodsInfoActivity;
import com.app.onebuy.user.MyAccountActivity;

import butterknife.BindView;
import butterknife.OnClick;
import my.ActivityTool;
import my.IntentUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

public class OrderSubmitActivity extends BasisActivity {


    @BindView(R.id.img_photo)
    ImageView img_photo;
    @BindView(R.id.text_goodsname)
    TextView text_goodsname;
    @BindView(R.id.text_leftchance)
    TextView text_leftchance;
    @BindView(R.id.text_order_num)
    TextView text_order_num;
    @BindView(R.id.text_price)
    TextView text_price;
    @BindView(R.id.text_paybyleft_des)
    TextView text_paybyleft_des;
    @BindView(R.id.view_paybyleft)
    View view_paybyleft;
    @BindView(R.id.view_paybywechat)
    View view_paybywechat;
    @BindView(R.id.view_paybypaypal)
    View view_paybypaypal;
    @BindView(R.id.view_paymaya)
    View view_paymaya;
    @BindView(R.id.view_paynamics)
    View view_paynamics;

    UserAccountInfoBean userAccountInfoBean;
    GoodsTypeBean goodsTypeBean;

    public static final String PAY_TYPE_BALANCE = "balance_pay", PAY_TYPE_WECHAT = "wechat",PAY_TYPE_PAYPAL = "paypal",PAY_TYPE_GLOBELABS = "globelabs",PAY_TYPE_MAYA = "paymaya",PAY_TYPE_NAMICS = "paynamics";
    String pay_type = PAY_TYPE_BALANCE;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.order_submit_activity);
        setTitle(R.string.order_submit_title);
        setTitleLeftButton(R.drawable.base_icon_back_white, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(GoodsInfoActivity.refreshNumber);
                finish();
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        goodsTypeBean = (GoodsTypeBean) getIntent().getSerializableExtra("data");
        if (goodsTypeBean == null) {
            finish();
            return;
        }
        setView();
        getAccountInfo();
    }


    private void getAccountInfo() {
        MyParams params = new MyParams();
        showProgress();
        MyHttpUtils.post(Constants.URL_USER_ACCOUNT_INFO, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if(isFinishing())return;
                        userAccountInfoBean = (UserAccountInfoBean) result;
                        setView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if(isFinishing())return;
                        hideLoading();
                    }
                },
                0, UserAccountInfoBean.class);

    }

    void setView() {
        if(isFinishing())return;
        BasisApp.loadImg(mContext, goodsTypeBean.getImg_url(), img_photo,R.mipmap.img_item_small_default);
        text_goodsname.setText(goodsTypeBean.getGoods_name());
        text_leftchance.setText(getResources().getString(R.string.order_goods_left_changce, goodsTypeBean.getLeftNum()));
        setOrderNumView();
        setPayTypeView();
        if (userAccountInfoBean == null) return;
        text_paybyleft_des.setText(getResources().getString(R.string.order_pay_byleftdes, (userAccountInfoBean.getUsable_account())+getString(R.string.unit_money)));

    }

    void setOrderNumView() {
        text_price.setText(goodsTypeBean.getTotalPrice()+getString(R.string.unit_money));
        text_order_num.setText(goodsTypeBean.getOrder_num() + "");
    }

    void setPayTypeView() {
        view_paybyleft.setSelected(false);
        view_paybywechat.setSelected(false);
        view_paybypaypal.setSelected(false);
        switch (pay_type){
            case PAY_TYPE_BALANCE:
                view_paybyleft.setSelected(true);
                break;
            case PAY_TYPE_WECHAT:
                view_paybywechat.setSelected(true);
                break;
            case PAY_TYPE_PAYPAL:
                view_paybypaypal.setSelected(true);
                break;
            case PAY_TYPE_MAYA:
                view_paymaya.setSelected(true);
                break;
        }
    }

    @OnClick(R.id.img_plus)
    public void onPlusClick() {
        goodsTypeBean.plusNum();
        setOrderNumView();
    }

    @OnClick(R.id.img_minus)
    public void onMinusClick() {
        goodsTypeBean.minusNum();
        setOrderNumView();
    }

    @OnClick(R.id.btn_five)
    public void btnFive() {
        goodsTypeBean.setOrderNum(5);
        setOrderNumView();
    }

    @OnClick(R.id.btn_ten)
    public void btnTen() {
        goodsTypeBean.setOrderNum(10);
        setOrderNumView();
    }

    @OnClick(R.id.btn_twenty)
    public void btnTwenty() {
        goodsTypeBean.setOrderNum(20);
        setOrderNumView();
    }

    @OnClick(R.id.btn_fifty)
    public void btnFifty() {
        goodsTypeBean.setOrderNum(50);
        setOrderNumView();
    }

    @OnClick(R.id.view_paybyleft)
    public void paybyleft() {
        pay_type = PAY_TYPE_BALANCE;
        setPayTypeView();
    }

    @OnClick(R.id.view_paybywechat)
    public void paybywechat() {
        pay_type = PAY_TYPE_WECHAT;
        setPayTypeView();
    }
    @OnClick(R.id.view_paybypaypal)
    public void paybypaypal() {
        pay_type = PAY_TYPE_PAYPAL;
        setPayTypeView();
    }
    @OnClick(R.id.view_globelabs)
    public void paybyglobelabs() {
        pay_type = PAY_TYPE_GLOBELABS;
        setPayTypeView();
    }

    @OnClick(R.id.view_paymaya)
    public void paymaya() {
        pay_type = PAY_TYPE_MAYA;
        setPayTypeView();
    }
    @OnClick(R.id.view_paynamics)
    public void paynamics() {
        pay_type = PAY_TYPE_NAMICS;
        setPayTypeView();
    }

    @OnClick(R.id.btn_charge)
    public void toCharge() {
        ActivityTool.skipActivity(mContext, MyAccountActivity.class);
    }

    @OnClick(R.id.btn_ok)
    public void checkAndSubmit() {

        final MyParams params = new MyParams();
        params.put("period_id", goodsTypeBean.getPeriod_id());
        params.put("number", goodsTypeBean.getOrder_num());
        params.put("pay_type", pay_type);
        showProgress();
        MyHttpUtils.post(Constants.URL_ORDER_SUBMIT, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
//                        DialogUtils.DialogOKmsgFinish(mContext, ((OrderListData) result).getStatusInfo());
                        if(isFinishing())return;
                        UserChargeHistoryListData bean = (UserChargeHistoryListData) result;
                        Bundle bundle = new Bundle();
                        bundle.putString("pay_type",bean.getPay_type());
                        bundle.putString("number",text_price.getText().toString().trim());
                        if(pay_type.equals(PAY_TYPE_BALANCE)){
                            showToast(bean.getStatusInfo());
                            ActivityTool.skipActivityForResult(mContext,PayCompleteActivity.class,bundle,0);
                            hideLoading();
                        }else if(pay_type.equals(PAY_TYPE_PAYPAL)){
                            getPayPalInfo(bean);
                        }else if(pay_type.equals(PAY_TYPE_GLOBELABS)){
                            paypalInfoBean = new OrderPaypalInfoBean();
                            IntentUtils.ViewUrl(mContext,bean.getPay_url());
                            setResult(Activity.RESULT_OK);
//                            finish();
                        }else if(pay_type.equals(PAY_TYPE_MAYA)){
                            getPayMayaInfo(bean);
                        }else if(pay_type.equals(PAY_TYPE_NAMICS)){
                            getPayNamicsInfo(bean);
                        }

                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        if(isFinishing())return;
                        hideLoading();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if(isFinishing())return;
                    }
                },
                0, UserChargeHistoryListData.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK){
            setResult(Activity.RESULT_OK);
            finish();
            return;
        }
//        PayPalUtil.getInstance().confirmPayResult(mContext, requestCode, resultCode, data, new PayPalUtil.DoResult() {
//            @Override
//            public void confirmSuccess(String id) {
//                //paypay支付成功，获取到支付id，需提交到后台
//            }
//
//            @Override
//            public void confirmNetWorkError() {
//
//                showToast(R.string.order_pay_faied);
//            }
//
//            @Override
//            public void customerCanceled() {
//                showToast(R.string.order_pay_faied);
//            }
//
//            @Override
//            public void confirmFuturePayment() {
//
//            }
//
//            @Override
//            public void invalidPaymentConfiguration() {
//                showToast(R.string.order_pay_faied);
//            }
//        });
    }

    OrderPaypalInfoBean paypalInfoBean;
    public void getPayPalInfo(UserChargeHistoryListData data){
        MyParams params = new MyParams();
        showProgress();
        params.put("order_sn", data.getOrder_sn());
        MyHttpUtils.post(Constants.URL_ORDER_PAY_BY_PAYPAL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if(isFinishing())return;
                        paypalInfoBean = (OrderPaypalInfoBean) result;
                        IntentUtils.ViewUrl(mContext,paypalInfoBean.getApproval_url());
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                        if(isFinishing())return;
                    }
                },
                0, OrderPaypalInfoBean.class);
    }


    public void getPayMayaInfo(UserChargeHistoryListData data){
        MyParams params = new MyParams();
        showProgress();
        params.put("order_sn", data.getOrder_sn());
        MyHttpUtils.post(Constants.URL_API+"order/paymaya_pay", params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if(isFinishing())return;
                        paypalInfoBean = (OrderPaypalInfoBean) result;
                        IntentUtils.ViewUrl(mContext,paypalInfoBean.getPay_url());
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if(isFinishing())return;
                        dismissProgress();
                    }
                },
                0, OrderPaypalInfoBean.class);
    }


    public void getPayNamicsInfo(UserChargeHistoryListData data){
        MyParams params = new MyParams();
        showProgress();
        params.put("order_sn", data.getOrder_sn());
        MyHttpUtils.post(Constants.URL_API+"order/paynamics_pay", params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if(isFinishing())return;
                        paypalInfoBean = (OrderPaypalInfoBean) result;
                        IntentUtils.ViewUrl(mContext,paypalInfoBean.getPay_url());
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if(isFinishing())return;
                        dismissProgress();
                    }
                },
                0, OrderPaypalInfoBean.class);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(paypalInfoBean != null){
            //请求接口查询支付结果
            setResult(Activity.RESULT_OK);
            finish();
//            DialogUtils.DialogOKmsgFinish(mContext,getString(R.string.pay_complete));
        }
    }
}
